<?php

use swap\visitor;
use swap\lazy_checker;
use swap\check_failed;

class certificate_controller extends \swap\controller {

    public static function before_filters() {
        $resume_id = g_int('id');
        return array(
            'resume_filter' => array(
                '*' => array('id' => $resume_id),
            ),
        );
    }

    public static function before_run() {
        self::role('user', 'redirect_to', 'my-site/login');
    }

    public static function index_action() {
        // 获取已有的证书
        $certificates = certificate_model::get_certificates_by_resume_id(visitor::get_value('id'));
        self::set('certificates', $certificates);

        self::show_page();
    }

    public static function new_action() {
        // none
    }
    
    public static function do_new_action() {
        // 拦截
        self::csrf('user');
        self::method('post');

        try {
            $resume_id = visitor::get_value('id');
            
            // 校验
            $props = self::check_form(p(), 'new');
            
            // 检查
            resume_ext_service::can_set_resume_ext($resume_id, 'certificate');
            
            // 执行
            $certificate_id = certificate_model::add($props);
            resume_ext_service::set_resume_ext($resume_id, 'certificate', $certificate_id);
            
            // 正常
            self::json_result(true, '证书添加成功');
        } catch (resume_except $se) {
            // 异常
            self::json_result(false, $se->get_msgs());
        } catch (check_failed $e) {
            // 异常
            self::json_result(false, $e->get_reasons());
        }
    }

    public static function edit_action() {
        // 检查certificate_id
        $certificate = self::check_certificate_id();
        self::set('certificate', $certificate);
        
        self::show_page();
    }

    public static function do_edit_action() {
        // 检查certificate_id
        $certificate = self::check_certificate_id();
        
        // 拦截
        self::csrf('user');
        self::method('post');
        
        try {
            // 校验
            $props = self::check_form(p(), 'edit');
            
            // 执行
            certificate_model::set_by_id($props, $certificate->id);
            
            // 正常
            self::json_result(true, '证书编辑成功');
        } catch (check_failed $e) {
            // 异常
            self::json_result(false, $e->get_reasons());
        }
    }

    public static function del_action() {
        // none
    }

    public static function do_del_action() {
        // 检查certificate_id
        $certificate = self::check_certificate_id();
        
        // 拦截
        self::csrf('user');
        
        // 执行
        certificate_model::del_by_id($certificate->id);
        resume_ext_service::del_resume_ext(visitor::get_value('id'), 'certificate', $certificate->id);
        
        // 成功
        self::json_result(true);
    }
    
    protected static function check_form($post, $operation) {
        $checker = new lazy_checker($post);
        $checker->check_model_rules('certificate', $operation);
        return $checker->get_all();
    }
    
    protected static function check_certificate_id() {
        $certificate_id = g_int('certificate_id');
        $certificate = certificate_model::get_by_id($certificate_id);
        if ($certificate === null) {
            self::forward_404('非法请求');
        }
        return $certificate;
    }
    
}